
function initMainTabs() {
    $("#main-tabs").tabs().bind("tabsshow",function (event, ui) {
        primeMainTabAccordion(ui.panel);
    });
    $('#main-tabs ul li a').click(function(){
        this.blur();
    });
    var selected = $("#main-tabs").tabs("option","selected");
    primeMainTabAccordion(selected == 0 ? $("#lexicon") : $("#interlinear"));
}

function initUnitTestingTab() {
    $("#unit-test").button().click(function() {
        $("#unit-results").show(400,function() {
            // Begin performing Unit Tests.
        });
        return false;
    });
    $("#unit-results").hide();
}

function primeMainTabAccordion(panel) {
    var el = $(".navaccordion",panel);
    if (el.data("primed")) return;
    el.accordion({
        header: 'h3',
        fillSpace: true,
        autoHeight: false
    });
    el.data("primed",true);
}

function intLineWrap(cssclass,text) {
    return '<div class="' + cssclass + '">' + text + '</div>';
}

function glsSmallCapitalize(gls) {
    var s = [];
    var set = gls.split("-");
    for (i in set) {
        var sub = set[i].split("=");
        for (j in sub) {
            var gloss = sub[j];
            if (gloss == gloss.toUpperCase())
                s.push('<span class="cap">' + gloss + '</span>');
            else
                s.push(gloss);
            if (j < sub.length - 1) s.push("=");
        }
        if (i < set.length - 1) s.push("-");
    }
    return s.join("");
}

function insertIntWordform(txt,mor,gls,trn,verse) {
    if (!mor) mor = "***";
    if (!gls) gls = "***";
    if (!trn) trn = "***";
    var intID = verse.attr("id") + "-" + ($(".int",verse).length + 1);
    var content = '<div class="int" id="' + intID + '">' + intLineWrap("txt",txt) + intLineWrap("mor",mor) + intLineWrap("gls",glsSmallCapitalize(gls)) + intLineWrap("trn",trn) + '</div>';
    var last = $(".free",verse);
    if (last.length > 0) {
        last.before(content);
        return;
    }
    verse.append(content);
}

function insertIntFreeTranslation(trn,verse) {
    verse.append('<div class="free">' + trn + '</div>');
}
